package HomeWork01;
/*
​ （1）创建一个Rectangle[]数组，长度为5

​ （2）并创建5个Rectangle的矩形对象，放到数组中，矩形的长和宽随机生成[1,10.0)范围内的

​ （3）遍历显示

​ （4）按照面积从小到大排序

​ （5）再次遍历显示
 */
import java.util.Random;
public class Test03 {
    public static void main(String[] args) {
        Random b=new Random();
        Rectangle a[]=new Rectangle[5];
        a[0]=new Rectangle(b.nextInt(10)+1,b.nextInt(10)+1);
        a[1]=new Rectangle(b.nextInt(10)+1,b.nextInt(10)+1);
        a[2]=new Rectangle(b.nextInt(10)+1,b.nextInt(10)+1);
        a[3]=new Rectangle(b.nextInt(10)+1,b.nextInt(10)+1);
        a[4]=new Rectangle(b.nextInt(10)+1,b.nextInt(10)+1);

        //遍历
        for (int i = 0; i < a.length; i++) {
            for(int j=0;j<a.length-1;j++){
                if(area(a[j].getLength(),a[j].getWidth())>area(a[j+1].getLength(),a[j+1].getWidth())){
                    Rectangle temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.println("第"+(i+1)+"个矩形的长为:"+a[i].getLength()+"宽为:"+a[i].getWidth());
        }
    }
    //面积方法
    public static double area(double length,double width){
        double area=length*width;
        return area;
    }
}
class Rectangle{
    private double length;
    private double width;
    //

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    public Rectangle() {
    }
    //

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }
}
